package simi;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.io.Util;

public class IOUtil{
	private static byte[] ERROR_BYTES = "There was an unexpected exception. The session was closed ".getBytes();
	public IOUtil(){
		super();
	}
	
	public void readWrite
		(final InputStream remoteInput, final OutputStream localOutput) {
	             	
		Thread writer = new Thread(){
			public void run(){
				try{
					Util.copyStream(remoteInput, localOutput);
					yield();
				} catch (IOException e){
					//e.printStackTrace();
					// Set appropriate error message
					// and exit the thread
					try{
						localOutput.write(ERROR_BYTES);	
					}catch(Exception e2){ 
						System.out.println("IOUtil run exception: " + e2.getMessage());
					}finally{
						//System.out.println("finally, guess I am done...");
					}				
				}			
				//System.out.println("Listen thread done, exiting...");
			}};
		writer.setPriority(Thread.currentThread().getPriority() + 1);
		writer.start();
	}
}